package indi.xliuovo.coursedesign.musicplayer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

/**
 * Created with Android Studio
 *
 * @Author xliuOvO
 */
public class RegisterFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    View registerFragment;
    EditText inputPhone, inputPassword, confirmPassword;
    Button btnNext;

    private String mParam1;
    private String mParam2;

    public RegisterFragment() {
    }

    public static RegisterFragment newInstance(String param1, String param2) {
        RegisterFragment fragment = new RegisterFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        registerFragment = inflater.inflate(R.layout.fragment_register, container, false);
        return registerFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWidget();
        // 为按钮注册点击事件
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEmpty()) { // 输入非空
                    if (isFull()) { // 手机号为11位
                        if (isSame()) { // 两次密码一致
                            QuestionFragment questionFragment = new QuestionFragment();
                            // 获取并传递数据
                            Bundle bundle = new Bundle();
                            bundle.putString("phoneNum", inputPhone.getText().toString());
                            bundle.putString("password", confirmPassword.getText().toString());
                            questionFragment.setArguments(bundle);
                            getActivity().getSupportFragmentManager().beginTransaction().
                                    replace(R.id.container, questionFragment).commit();
                        }
                    }
                }
            }
        });
    }

    // 初始化控件
    void initWidget() {
        inputPhone = registerFragment.findViewById(R.id.inputPhone);
        inputPassword = registerFragment.findViewById(R.id.inputPassword);
        confirmPassword = registerFragment.findViewById(R.id.confirmPassword);
        btnNext = getActivity().findViewById(R.id.nextStep);
    }

    // 判断输入项是否有空
    boolean isEmpty() {
        if (TextUtils.isEmpty(inputPhone.getText()) || TextUtils.isEmpty(inputPassword.getText().toString()) ||
                TextUtils.isEmpty(confirmPassword.getText().toString())) {
            Toast.makeText(getContext(), "有输入项为空", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }

    // 判断手机号是否为11位
    public boolean isFull() {
        if (inputPhone.getText().length() == 11)
            return true;
        else {
            Toast.makeText(getContext(), "手机号不足11位", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    // 判断两次输入密码是否一致
    boolean isSame() {
        if (inputPassword.getText().toString().equals(confirmPassword.getText().toString())) {
            return true;
        } else {
            Toast.makeText(getContext(), "密码不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}